home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / DefaultTvEventHandler.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  8.9 KB  |  260 lines

  1. /*
  2.  * DefaultTvEventHandler.java   1.0   12 Jan 1997
  3.  *
  4.  * Copyright (c) 1996 Krumel & Associates, Inc.  All Rights Reserved.
  5.  *
  6.  * This software is provided as is.  Krumel & Associates shall not be liable
  7.  * for any damages suffered by licensee as a result of using, modifying or
  8.  * distributing this software or its derivatives.
  9.  */
  10.  
  11. package symantec.itools.db.awt;
  12.  
  13. import java.awt.*;
  14.  
  15. public class DefaultTvEventHandler implements TvEventHandler {
  16.     Grid            grid;
  17.  
  18.     int             rowSelectionBase = -1;
  19.  
  20.     public void setupView(Grid v) {
  21.         grid = v;
  22.     }
  23.  
  24.     public boolean handleCellEvent(Event e, TableCell cell) {
  25.         if (e.id == Grid.CELL_KEY_UP || e.id == Grid.LOST_CELL_FOCUS) {
  26.             return true;
  27.         }
  28.  
  29.         grid.clearAllSelections();
  30.         return true;
  31.     }
  32.  
  33.     public boolean handleColHeadingEvent(Event e, TableCell cell) {
  34.         //remember - rows and columns are measured as 0 relative inside grid
  35.         //           so must adjust before calling selection functions
  36.  
  37.         //we don't do anything for col heading events
  38.         return true;
  39.     }
  40.  
  41.     public boolean handleRowHeadingEvent(Event e, TableCell cell) {
  42.  
  43.         //remember - rows and columns are measured as 0 relative inside grid
  44.         //           so must adjust before calling selection functions
  45.         if (e.id == Grid.BUTTON_UP_EVENT) {
  46.             boolean leftPressed = (e.modifiers & e.ALT_MASK & e.META_MASK) == 0;
  47.             if (e.shiftDown() && leftPressed) {
  48.                 if (grid.isSelectionBaseSet()) {
  49.                     //start a selection
  50.                     grid.selectRange(cell.row() + 1, 0);
  51.                 } else {
  52.                     grid.setSelectionBase(cell.row()+1, 0);
  53.                     grid.selectRange(cell.row() + 1, 0);
  54.                 }
  55.             } else if (e.controlDown() && leftPressed) {
  56.                 if (!grid.isRangeSelected()) {
  57.                     grid.setSelectionBase(cell.row()+1, 0);
  58.                 }
  59.  
  60.                 grid.toggleRow(cell.row() + 1);
  61.             } else if (leftPressed) {
  62.                 boolean set = grid.isRowSet(cell.row()+1);
  63.                 boolean auto = grid.getAutoRedraw();
  64.                 grid.setAutoRedraw(false);
  65.                 grid.clearAllSelections();
  66.                 grid.setSelectionBase(cell.row()+1, 0);
  67.                 grid.setRow(cell.row() + 1, !set);
  68.                 grid.setAutoRedraw(auto);
  69.                 grid.redrawAsync();
  70.             }
  71.         }
  72.  
  73.         return true;
  74.     }
  75.  
  76.     public boolean handleCornerCellEvent(Event e, TableCell cell) {
  77.         //remember - rows and columns are measured as 0 relative inside grid
  78.         //           so must adjust before calling selection functions
  79.  
  80.         //we don't do anything for this cell
  81.         return true;
  82.     }
  83.  
  84.     public boolean handleToolbarEvent(Event e) {
  85.         if (e.target instanceof java.awt.TextField && e.id == Event.ACTION_EVENT) {
  86.             java.awt.TextField go = (java.awt.TextField)e.target;
  87.             try {
  88.                 String rowText = go.getText();
  89.                 if (rowText.equals("")) { return true; }
  90.                 int row = Integer.parseInt(rowText);
  91.                 grid.gotoRow(row);
  92.             } catch(NumberFormatException ex) {
  93.                 handleException(null, ex);
  94.             }
  95.         }
  96.  
  97.         if (!(e.target instanceof Button) ||
  98.             (e.target instanceof Button && e.id != Event.ACTION_EVENT))
  99.         {
  100.             return false;
  101.         }
  102.  
  103.         Button button = (Button)e.target;
  104.         String label = button.getLabel();
  105.  
  106.         if (label.equals(Grid.gotoRowLabel)) {
  107.             try {
  108.                 String rowText = (String)e.arg;
  109.                 if (rowText.equals("")) { return true; }
  110.                 int row = Integer.parseInt(rowText);
  111.                 grid.gotoRow(row);
  112.             } catch(NumberFormatException ex) {
  113.                 handleException(null, ex);
  114.             }
  115.         } else if (label.equals(Grid.appendRowLabel)) {
  116.             try {
  117.                 grid.appendRow();
  118.             } catch(TypeNotSupported ex) {
  119.                 handleException(null, ex);
  120.             }
  121.  
  122.             return true;
  123.         } else if (label.equals(Grid.deleteRowLabel)) {
  124.             int vv=1;
  125.             int actrow=1;
  126.           
  127.             try{
  128.             actrow=grid.currSelectedCell.row()+1;
  129.             
  130.             }
  131.             catch(NullPointerException ex){vv=0;}
  132.             int rows[] = grid.getSelectedRows();
  133.             boolean auto = grid.getAutoRedraw();
  134.             grid.setAutoRedraw(false);
  135.  
  136.             try {
  137.                 if (rows.length == 0) {
  138.                     //this is zero relative so must adjust to call API
  139.                     Coordinate c = grid.getCurrentCellCoordinates();
  140.                     if (c != null) {
  141.                         grid.deleteRow(c.row());
  142.                         grid.setFocusToRow(0);
  143.                         // grid.redrawAsync();;
  144.                     }
  145.                 } else {
  146.                     for(int i=rows.length-1; i>=0; i--) {
  147.                             grid.deleteRow(rows[i]);
  148.                     }
  149.                 }
  150.             } catch(TypeNotSupported ex) {
  151.                 handleException(null, ex);
  152.             }
  153.             if(vv==0){
  154.             grid.currSelectedCell.setRow(0);
  155.             grid.setFocusToRow(0);}
  156.             if(vv!=0){
  157.             grid.setFocusToRow(actrow);}
  158.  
  159.             grid.setAutoRedraw(auto);
  160.             grid.clearAllSelections();
  161.             grid.redraw();
  162.             return true;
  163.         } else if (label.equals(Grid.insertRowLabel)) {
  164.             int row = grid.getFirstSelectedRow();
  165.             try {
  166.                 grid.insertRow(row);
  167.             } catch(TypeNotSupported ex) {
  168.                 handleException(null, ex);
  169.             }
  170.  
  171.             // grid.clearAllSelections();
  172.             return true;
  173.         } else if (label.equals(Grid.saveLabel)) {
  174.             try {
  175.                 grid.save();
  176.                 // grid.redrawAsync();
  177.             } catch(TypeNotSupported ex) {
  178.                 handleException(null, ex);
  179.             }
  180.  
  181.             return true;
  182.         } else if (label.equals(Grid.restartLabel)) {
  183.             grid.restart();
  184.             // grid.redrawAsync();
  185.             return true;
  186.         } else if (label.equals(Grid.undoRowLabel)) {
  187.             int rows[] = grid.getSelectedRows();
  188.             boolean auto = grid.getAutoRedraw();
  189.             grid.setAutoRedraw(false);
  190.  
  191.             try {
  192.                 if (rows.length == 0) {
  193.                     //this is zero relative so must adjust to call API
  194.                     Coordinate c = grid.getCurrentCellCoordinates();
  195.                     if (c != null) {
  196.                         grid.undoRow(c.row());
  197.                     }
  198.                 } else {
  199.                     for(int i=rows.length-1; i>=0; i--) {
  200.                             grid.undoRow(rows[i]);
  201.                     }
  202.                 }
  203.             } catch(TypeNotSupported ex) {
  204.                 handleException(null, ex);
  205.             }
  206.  
  207.             grid.setAutoRedraw(auto);
  208.             grid.clearAllSelections();
  209.             // grid.redrawAsync();;
  210.             return true;
  211.         } else if (label.equals(Grid.undeleteRowLabel)) {
  212.             int vv=1;
  213.             int actrow=1;
  214.             try{
  215.             actrow=grid.currSelectedCell.row()+1;
  216.             }
  217.             catch(NullPointerException ex){vv=0;}
  218.             int rows[] = grid.getSelectedRows();
  219.             boolean auto = grid.getAutoRedraw();
  220.             grid.setAutoRedraw(false);
  221.  
  222.             try {
  223.                 if (rows.length == 0) {
  224.                     //this is zero relative so must adjust to call API
  225.                     Coordinate c = grid.getCurrentCellCoordinates();
  226.                     if (c != null) {
  227.                         grid.undeleteRow(c.row());
  228.                         // grid.redrawAsync();;
  229.                     }
  230.                 } else {
  231.                     for(int i=rows.length-1; i>=0; i--) {
  232.                             grid.undeleteRow(rows[i]);
  233.                     }
  234.                 }
  235.             } catch(TypeNotSupported ex) {
  236.                 handleException(null, ex);
  237.             }
  238.             if(vv==0){
  239.             grid.currSelectedCell.setRow(0);
  240.             grid.setFocusToRow(0);
  241.             grid.requestFocus();}
  242.             if(vv!=0){
  243.             grid.setFocusToRow(actrow);}
  244.             grid.setAutoRedraw(auto);
  245.             grid.clearAllSelections();
  246.             grid.redraw();
  247.             grid.requestFocus();
  248.             return true;
  249.         }
  250.  
  251.         return true;
  252.     }
  253.  
  254.     public void handleException(Coordinate pos, Exception exc) {
  255.         if (pos != null) {
  256.             System.out.println("Error occurred in Grid at row="+pos.row()+" col="+pos.col());
  257.         }
  258.         exc.printStackTrace();
  259.     }
  260. }